package com.youi.shop.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.youi.shop.commons.exceptions.GlobalException;
import com.youi.shop.commons.result.RespEntity;
import com.youi.shop.commons.result.StatusCode;
import com.youi.shop.mapper.CompanyAddressMapper;
import com.youi.shop.pojo.entity.CompanyAddress;
import com.youi.shop.pojo.qo.address.AddressQO;
import com.youi.shop.pojo.vo.address.MemberAddressVO;
import com.youi.shop.pojo.vo.address.UserAddressVO;
import com.youi.shop.service.CompanyAddressService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 地址表 服务实现类
 * </p>
 *
 * @author xxf
 * @since 2020-11-09
 */
@Service
public class CompanyAddressServiceImpl extends ServiceImpl<CompanyAddressMapper, CompanyAddress> implements CompanyAddressService {

    @Resource
    CompanyAddressMapper companyAddressMapper;

    /**
     * 根据memberId查询地址信息
     * @param memberId
     * @return
     */
    @Override
    public RespEntity<List<MemberAddressVO>> selectByMemberId(Long memberId) {
        //条件构造
        QueryWrapper<CompanyAddress> wrapper = new QueryWrapper<>();
        wrapper.eq("member_id", memberId)
                .eq("del_status", 0);
        //查询数据库
        List<CompanyAddress> addressList = companyAddressMapper.selectList(wrapper);
        //判断是否有查询结果
        if (addressList == null || addressList.size() == 0) {
            throw new GlobalException(StatusCode.ADDRESS_NULL_ERROR);
        } else {
            List<MemberAddressVO> list = new ArrayList<MemberAddressVO>();
            //遍历查询结果
            for (CompanyAddress companyAddress : addressList) {
            MemberAddressVO memberAddressVO = new MemberAddressVO();
                BeanUtils.copyProperties(companyAddress, memberAddressVO);
                list.add(memberAddressVO);
            }
            return RespEntity.success(list);
        }
    }

    /**
     * 根据userId查询地址信息
     * @param userId
     * @return
     */
    @Override
    public RespEntity<List<UserAddressVO>> selectByUserId(Long userId) {
        //条件构造
        QueryWrapper<CompanyAddress> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", userId)
                .eq("del_status", 0);
        //查询数据库
        List<CompanyAddress> addressList = companyAddressMapper.selectList(wrapper);
        //判断是否有查询结果
        if (addressList == null || addressList.size() == 0) {
            throw new GlobalException(StatusCode.ADDRESS_NULL_ERROR);
        } else {
            List<UserAddressVO> list = new ArrayList<UserAddressVO>();
            //遍历查询结果
            for (CompanyAddress companyAddress : addressList) {
                UserAddressVO userAddressVO = new UserAddressVO();
                BeanUtils.copyProperties(companyAddress, userAddressVO);
                list.add(userAddressVO);
            }
            return RespEntity.success(list);
        }
    }

    /**
     * 添加一条地址信息
     * @param addressQO
     * @return
     */
    @Override
    public boolean insert(AddressQO addressQO) {
        //给持久化对象赋值
        CompanyAddress address = new CompanyAddress();
        BeanUtils.copyProperties(addressQO, address);
        boolean flag = false;
        //插入数据库
        int res = companyAddressMapper.insert(address);
        if (res > 0) {
            flag = true;
        }

        return flag;
    }

    /**
     * 会员更新一条地址信息
     * @param addressQO
     * @return
     */
    @Override
    public CompanyAddress updateById(AddressQO addressQO) {
        //查询是否有该会员
        CompanyAddress companyAddress = companyAddressMapper.selectById(addressQO.getAddressId());

        //没有返回null
        if (companyAddress == null) {
            return null;
        }
        //有该会员
        //给持久化对象赋值
        CompanyAddress address = new CompanyAddress();
        BeanUtils.copyProperties(addressQO, address);

        //插入数据库
        int res = companyAddressMapper.updateById(address);
        if (res > 0) {
//            MemberAddressVO memberAddressVO = new MemberAddressVO();
//            BeanUtils.copyProperties(address, memberAddressVO);
            return address;
        }

        return null;
    }

    /**
     * 商家更新一条地址信息
     * @param addressQO
     * @return
     */
//    @Override
//    public UserAddressVO updateById(AddressQO addressQO,int a) {
//        //查询是否有该商家
//        CompanyAddress companyAddress = companyAddressMapper.selectById(addressQO.getUserId());
//        //没有返回null
//        if (companyAddress == null) {
//            return null;
//        }
//
//        //给持久化对象赋值
//        CompanyAddress address = new CompanyAddress();
//        BeanUtils.copyProperties(addressQO, address);
//
//        //插入数据库
//        int res = companyAddressMapper.updateById(address);
//        if (res > 0) {
//            UserAddressVO userAddressVO = new UserAddressVO();
//            BeanUtils.copyProperties(address, userAddressVO);
//            return userAddressVO;
//        }
//
//        return null;
//    }

    /**
     * 根据id逻辑删除
     * @param addressIds
     * @return
     */
    @Override
    public boolean deleteByIds(List<Long> addressIds) {
        boolean flag =false;
        int res = companyAddressMapper.deleteBatchIds(addressIds);
        //删除成功
        if (res > 0) {
            flag = true;
        }
        return flag;
    }
}
